<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();
class Parser_Tag_video extends Parser {
	public $tags_inside = true;
public $allowBr = false;
	public function get($inner,$param = ''){
		$inner = Security::cleanXSSUrl($inner);
		$host = getHost($inner);
		switch ($host){
			case 'youtube.com':
				$videocode = $this->insertYoutube($inner);
				break;
			case 'rutube.ru':
				$videocode = $this->insertRutube($inner);
				break;
			default:
				$videocode = $this->insertDefault($inner);
				break;
		}
		
		return '<!--video:'.$inner.'-->'.$videocode.'<!--/video-->';
	}
	
	public function unparse($text){
		return preg_replace('/\<\!--video:(.+?)--\>.+?\<\!--\/video--\>/is','[video]\\1[/video]',$text);
	}
	
	private function insertYoutube($url){
		$id = $this->youtubeGetId($url);
		
		return '<object width="640" height="385">'.
		'<param name="movie" value="http://www.youtube.com/v/'.$id.'&amp;hl=en_GB&amp;fs=1?rel=0"></param>'.
		'<param name="allowFullScreen" value="true"></param>'.
		'<param name="allowscriptaccess" value="always"></param>'.
		'<embed src="http://www.youtube.com/v/'.$id.'&amp;hl=en_GB&amp;fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385">'.
		'</embed>'.
		'</object>';
	}
	private function insertRutube($url){
		$id = $this->youtubeGetId($url);
		return '<object width="470" height="353">'.
		'<param name="movie" value="http://video.rutube.ru/'.$id.'"></param>'.
		'<param name="wmode" value="window"></param>'.
		'<param name="allowfullscreen" value="true"></param>'.
		'<embed src="http://video.rutube.ru/'.$id.'" type="application/x-shockwave-flash" wmode="window" width="470" height="353" allowfullscreen="true" >'.
		'</embed>'.
		'</object>';
	}
	
	private function insertDefault($url){
		$url = urlencode($url);
		$uid = uniqid('vid_');
		$config = 'config={
		"clip":{
			"url":"'.$url.'","scaling":"fit","autoPlay":false,"autoBuffering":false
		},
		"playlist":[
			{"url":"'.$url.'"}
		],
		"canvas": {
			"backgroundColor":"#000"
		}
		}';
		$config = str_replace(array(R,N,"\t"),'',$config);
		return '<object width="400" height="300" id="'.$uid.'" name="undefined" data="'.LINK_ROOT.'scripts/swf/flowplayer-3.2.2.swf" type="application/x-shockwave-flash">'.
		'<param name="movie" value="'.LINK_ROOT.'scripts/swf/flowplayer-3.2.2.swf" />'.
		'<param name="allowfullscreen" value="true" />'.
		'<param name="autoplay" value="false" />'.
		'<param name="flashvars" value=\''.$config.'\' /></object>';
	}

	
	private function youtubeGetId($url){
		$url = rawurldecode($url);
			if ( is_int(strpos($url,'?v=')) ){
			$id = explode('?v=',$url);
			$id = explode('&',$id[1]);
			return $id[0];
		} elseif (is_int(strpos($url,'v/'))) {
			$id = explode('v/',$url);
			$id = explode('&',$id[1]);
			return $id[0];
		} else {
			return $url;
		}
	}
}